import { PlusOutlined } from '@ant-design/icons';
import { ProTable } from '@ant-design/pro-components';
import { Button, message } from 'antd';
import type { ActionType } from '@ant-design/pro-components';
import { useRef } from 'react';
import { useState } from 'react'
import getColumns from './columns';
import { getMedicineList, medicineImportExport } from '@/services/medicine'
import Modals, { ModalType } from './Modals'
import useRemind from './useRemind';
import { proTableFormat, sortTransform } from '@/utils/format'
import WithAuthComp from '@/components/WithAuthComp';
import { ROLE } from '@/common/const'
import { downloadFileWithToken } from '@/utils/download'


export default () => {
    const [exportLoading, setExportLoading] = useState(false)
    const modalsRef = useRef<any>()
    const actionRef = useRef<ActionType>();
    const columns = getColumns({ modalsRef });
    const onAddFinish = () => {
        modalsRef.current.hide(ModalType.DIALOG)
        actionRef.current?.reload(true);
    }
    const onInFinish = () => {
        modalsRef.current.hide(ModalType.PUTIN)
        actionRef.current?.reload(true);
    }
    useRemind()

    const inExport = async () => {
        setExportLoading(true)
        try {
            const blob = await medicineImportExport()
            // 获取当前日期
            const date = new Date().toLocaleDateString().replace(/\//g, '-')
            downloadFileWithToken(blob, '今日入库导出结果' + date + '.xlsx')
            message.success('导出成功')
        } catch (error) {
            message.error('导出失败，请刷新后尝试')
        } finally {
            setExportLoading(false)
        }
    }
    return (
        <>
            <ProTable
                columns={columns}
                cardBordered
                actionRef={actionRef}
                request={async (params, sort, filter) => {
                    const { pageInfo } = await getMedicineList({ ...params, currentPage: params.current, sort: sortTransform(sort) });
                    console.log(pageInfo, '===pageInfo===')
                    return proTableFormat(pageInfo)
                }}
                rowKey="name"
                search={{
                    labelWidth: 'auto',
                    span: 6
                }}
                options={{
                    setting: true,
                    reload: false
                }}
                pagination={{
                    pageSize: 10,
                }}
                dateFormatter="string"
                // 表格新建栏
                toolBarRender={() => [
                    <WithAuthComp authKey={[ROLE.RKY]}>
                        <Button
                            key="exportRest"
                            onClick={inExport}
                            type="primary"
                            loading={exportLoading}
                        >
                            今日入库
                        </Button>
                    </WithAuthComp>
                    ,
                    <WithAuthComp authKey={[ROLE.RKY, ROLE.CKY]}>
                        <Button
                            key="exportRest"
                            onClick={() => {
                                modalsRef.current.show(ModalType.EXPORT)
                            }}
                            type="primary"
                        >
                            结存导出
                        </Button>
                    </WithAuthComp>
                    ,
                    <Button
                        key="newMedicine"
                        icon={<PlusOutlined />}
                        onClick={() => {
                            modalsRef.current.show(ModalType.DIALOG)
                        }}
                        type="primary"
                    >
                        新建
                    </Button>,
                ]}
            />
            <Modals onAddFinish={onAddFinish} ref={modalsRef} onInFinish={onInFinish} actionRef={actionRef} />
        </>

    );
};